package com.shundr.common.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.shundr.frame.db.LDaoManager;
import com.shundr.frame.util.LLogger;
import com.shundr.route.model.TruckerRouteInfo;
/**
 * 常跑路线
 * @author Administrator
 *
 */
public class RouteDao {
private Dao<TruckerRouteInfo, Integer> routeDao;
	
	public RouteDao() {
		routeDao = LDaoManager.createDao(TruckerRouteInfo.class);
	}
	
	/**
	 * (这里描述这个方法适用条件 – 可选)
	 * @param parentCode
	 * @return
	 * List<TruckerRouteInfo>
	 * @exception
	 * @since  1.0.0
	 */
	public List<TruckerRouteInfo> getRouteList(Integer userId){
		try {
			QueryBuilder<TruckerRouteInfo, Integer> queryBuilder = routeDao.queryBuilder();
			queryBuilder.where().eq("userId", userId);
			//queryBuilder.orderBy("sort", false);
			return queryBuilder.query();
		} catch (SQLException e) {
			LLogger.e("创建数据类型时异常" + e);
		}
		return new ArrayList<TruckerRouteInfo>();
	}
	
	
	/**
	 * 插入单条数据
	 * insert(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param info
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void insert(TruckerRouteInfo info){
		try {
			routeDao.create(info);
		} catch (SQLException e) {
			LLogger.e("创建数据类型时异常" + e);
		}
	}
	
	/**
	 * 删除路线
	 * @param id
	 */
	public void delete(Integer id){
		try {
			routeDao.deleteById(id);
		} catch (SQLException e) {
			LLogger.e("删除路线时异常" + e);
		}
	}

	/**
	 * 批量插入常跑路线
	 * @param infos
	 */
	public void syncData(final List<TruckerRouteInfo> infos) {
		if(infos == null)
			return;
		try {
			routeDao.callBatchTasks(new Callable<Void>() {

				@Override
				public Void call() throws Exception {
					routeDao.deleteBuilder().delete();
					for(TruckerRouteInfo info : infos) {
						routeDao.create(info);
					}
					return null;
				}});
		} catch (Exception e) {
			LLogger.e("保存数据类型时发生异常 ： " + e);
		}
	}


}
